package com.app.tablet.view;

import com.app.tablet.R;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;

public class BaseActivity extends Activity {

	protected static final int DIALOG_ID_PROGRESS = 0x12AB1;

	/**
	 * Why are you overriding this method and what are you achieving here?
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

	}

	/**
	 * Hides the progress dialog
	 */
	public void hideProgress() {
		try {

			removeDialog(DIALOG_ID_PROGRESS);
		} catch (IllegalArgumentException iae) {
			// That's ok if the trying to hide the progress already hidden
		}
	}

	/**
	 * Overridden to create the progress and/or error dialogs
	 */
	protected Dialog onCreateDialog(int id) {
		hideProgress();
		switch (id) {
		case DIALOG_ID_PROGRESS:
			ProgressDialog dialog = new ProgressDialog(this);
			dialog.setMessage("Please Wait..");
			dialog.setIndeterminate(true);
			dialog.setCancelable(false);
			dialog.setOnKeyListener(new OnKeyListener() {

				@Override
				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					if (keyCode == KeyEvent.KEYCODE_SEARCH) {
						return true;
					}
					return false;
				}
			});
			return dialog;

		default:
			return super.onCreateDialog(id);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuinflater1 = getMenuInflater();
		menuinflater1.inflate(R.menu.addpagemenu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.search:
			Intent intent = new Intent(getApplicationContext(),
					SearchActivity.class);
			intent.setAction(Intent.ACTION_SEARCH);
			startActivity(intent);
			onSearchRequested();
			return true;

		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		MenuInflater menuinflater2 = getMenuInflater();
		menuinflater2.inflate(R.menu.addpagemenu, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.search:
			// Intent intent1 = new Intent(getApplicationContext(),
			// SearchActivity.class);
			// startActivity(intent1);
			break;
		}
		return super.onContextItemSelected(item);
	}

	/*
	 * @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch
	 * (keyCode) { case KeyEvent.KEYCODE_BACK: Toast.makeText(this,
	 * "Back Key clicked", 500).show(); return true; default: return
	 * super.onKeyDown(keyCode, event); } }
	 */

}
